package com.leancloud.login.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.aries.ui.view.title.TitleBarView;
import com.leancloud.login.NameConstant;
import com.leancloud.login.LoginPath;
import com.leancloud.login.R;
import com.leancloud.login.TimingTextView;
import com.leancloud.login.user.LoginBack;
import com.leancloud.login.user.UserGlobal;
import com.vise.xsnow.event.BusManager;

import tech.com.commoncore.base.BaseTitleActivity;
import tech.com.commoncore.event.LoginChangeEvent;
import tech.com.commoncore.others.PrivateProtocolActivity;
import tech.com.commoncore.others.UserAgreementActivity;
import tech.com.commoncore.others.UserPrivacyActivity;
import tech.com.commoncore.utils.FastUtil;
import tech.com.commoncore.utils.RegUtils;
import tech.com.commoncore.utils.ToastUtil;

/**
 * 注册
 */
@Route(path = LoginPath.LOGIN_REGISTER)
public class RegisterPhoneActivity extends BaseTitleActivity implements View.OnClickListener {
    private EditText phoneEt, verifyEt ,et_pw;
    private TimingTextView timingTextView;

    TextView protocol_tv;

    @Override
    public void setTitleBar(TitleBarView titleBar) {
        titleBar.setTextColor(Color.WHITE)
                .setTitleMainText(getResources().getString(R.string.regist_title))
                .setLeftTextDrawable(null)
                .setBgColor(Color.TRANSPARENT)
                .setDividerHeight(0)
                .setStatusBarLightMode(true)
                .setRightTextDrawable(R.drawable.login_icon_back)
                .setOnRightTextClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        finish();
                    }
                });
    }

    @Override
    public int getContentLayout() {
        return R.layout.register_test;
        //login_activity_phone_register
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        ((TextView) findViewById(R.id.input_banner_tx)).setText(getResources().getString(R.string.regist_title));
        et_pw = findViewById(R.id.login_input_pw_et);
        phoneEt = findViewById(R.id.login_input_phone_et);
        verifyEt = findViewById(R.id.login_input_verify_et);
        timingTextView = findViewById(R.id.login_input_verify_bt);
        protocol_tv = findViewById(R.id.protocol_tv);

        findViewById(R.id.btn_register).setOnClickListener(this);
        findViewById(R.id.protocol_tx).setOnClickListener(this);
        findViewById(R.id.protocol_tv).setOnClickListener(this);
        timingTextView.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btn_register) {
            doRegister();
        } else if (v.getId() == R.id.protocol_tx) {
            FastUtil.startActivity(mContext, UserAgreementActivity.class);
        } else if (v.getId() == R.id.login_input_verify_bt) {
            requestSmsCode();
        } else if (v.getId() == R.id.protocol_tv) {
            FastUtil.startActivity(mContext, PrivateProtocolActivity.class);
        }
    }

    private void doRegister() {
        final String phone = phoneEt.getText().toString().trim();
        final String verify = verifyEt.getText().toString().trim();
        final String pw = et_pw.getText().toString().trim();

        if (phone == null || phone.isEmpty() || !RegUtils.isMobile(phone)) {
            ToastUtil.show(getResources().getString(R.string.regist_phone));
            return;
        }

        if (verify == null || verify.isEmpty()) {
            ToastUtil.show(getResources().getString(R.string.regist_code));
            return;
        }
        if (pw == null || pw.isEmpty()) {
            ToastUtil.show(getResources().getString(R.string.regist_pw));
            return;
        }

        showLoading(getResources().getString(R.string.regist_logining));

//        signOrLogin(phone, verify);
        signOrLogin(phone, pw,verify);
    }

    private void signOrLogin(final String phone, final String pw, final String code) {
        UserGlobal.getUserImp().userRegistered(phone,pw,code, "", NameConstant.ApplyName,"1",new LoginBack() {
            @Override
            public void onBack(Object o, String err) {
                hideLoading();
                if (err == null) {
                    BusManager.getBus().post(new LoginChangeEvent(true));
                    finish();
                } else {
                    ToastUtil.show(getResources().getString(R.string.regist_login_fail));
                }
            }
        });

//        final BUser user = new BUser();
//        //设置手机号码（必填）
//        user.setMobilePhoneNumber(phone);
//        //设置用户名，如果没有传用户名，则默认为手机号码
//        user.setUsername(phone);
//        //设置额外信息：此处为年龄
//        user.signOrLogin(code, new SaveListener<BUser>() {
//            @Override
//            public void done(BUser bUser, BmobException e) {
//                if (e == null) {
//                    UserGlobal.getUserImp().userRegistered(phone,pw,code, "", "cncp0719","2",new LoginBack() {
//                        @Override
//                        public void onBack(Object o, String err) {
//                            hideLoading();
//                            if (err == null) {
//                                BusManager.getBus().post(new LoginChangeEvent(true));
//                                finish();
//                            } else {
//                                ToastUtil.show("登录失败,检查网络后重试");
//                            }
//                        }
//                    });
////                    UserGlobal.getUserImp().allianceLogin(user.getObjectId(), user.getMobilePhoneNumber(), new LoginBack() {
////                        @Override
////                        public void onBack(Object o, String err) {
////                            hideLoading();
////                            if (err == null) {
////                                BusManager.getBus().post(new LoginChangeEvent(true));
////                                finish();
////                            } else {
////                                ToastUtil.show("登录失败,检查网络后重试");
////                            }
////                        }
////                    });
//
//
//
//                } else {
//                    ToastUtil.show("短信注册或登录失败:" + e.getMessage() + "\n");
//                }
//            }
//        });
    }

    private void requestSmsCode() {
        String phone = phoneEt.getText().toString().trim();
        if (!RegUtils.isMobile(phone))
            return;

        UserGlobal.getUserImp().requestAllVerificationCode(phone, NameConstant.AppName,NameConstant.ApplyName, new LoginBack() {
            @Override
            public void onBack(Object o, String err) {
                hideLoading();
                if (err == null) {
//                    BusManager.getBus().post(new LoginChangeEvent(true));
//                    finish();
                } else {
                    ToastUtil.show(getResources().getString(R.string.regist_code_fail));
                }
            }
        });
//        BmobSMS.requestSMSCode(phone, "", new QueryListener<Integer>() {
//            @Override
//            public void done(Integer smsId, BmobException e) {
//                if (e == null) {
//
//                } else {
//                    ToastUtil.show("发送验证码失败,可能请求过于频繁");
//                }
//            }
//        });
    }
}
